﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Intorise.Common;
using Model;
using Config;
using Intorise.Net;
using System.Net;
using System.Threading.Tasks;
using System.Threading;

namespace MuBot
{
    static class Program
    {
        public static MainConfig AppConfig { get; private set; }

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            //Test(); return;
            SaveCompleteHelper helper = null;
            Form.CheckForIllegalCrossThreadCalls = false;
            System.Net.ServicePointManager.Expect100Continue = false;
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);


            ConfigManager.ApplicationConfig = new MainConfig();
            ConfigManager.BusinessConfig = new BotConfig();
            ConfigManager.Init();

            //for (var i = 0; i < 100; i++)
            //{
            //    ConsoleBLL.WriteInfo(GameHelper.GetName() + "\t");
            //}
            //Console.ReadLine();
            //return;

            //Application.Run(new MainForm());
            Application.Run(new MainWindow(args));
            // Application.Run(new CreateCharacterWindow());
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            ConsoleBLL.WriteLineError("出现未捕获的异常：\r\n" + e.ExceptionObject.ToString() + "\r\n" + e.ExceptionObject.GetType().FullName);
        }

        static void Test()
        {
            bool quit = false;
            Thread thread = null;
            var task = Task.Factory.StartNew(() =>
            {
                thread = Thread.CurrentThread;
                Console.WriteLine("Thread started.");
                try
                {
                    while (!quit)
                    {
                        Console.WriteLine("Thread sleep");
                        Thread.Sleep(10000);
                        Console.WriteLine("Thread resume");
                    }
                }
                catch (Exception ex)
                {
                    ConsoleBLL.WriteLineError(ex.Message);
                }
                thread = null;
                Console.WriteLine("Thread while quited.");
            });

            while (true)
            {
                Console.Write("CMD>");
                var input = Console.ReadLine();
                if (input.ToLower() == "quit")
                {
                    thread.Interrupt();
                    quit = true;
                    break;
                }
            }
            ConsoleBLL.WriteInfo("Press ENTER key to exit..."); Console.ReadLine();
        }
    }
}
